﻿using System;
using System.Threading.Tasks;

namespace Edu.ZJ
{
    public class LoadingUtil
    {
        private static LoadingForm form;
        private static readonly Object syncLock = new Object();  //加锁使用

        public static void Show(string text)
        {
            Task.Factory.StartNew(() =>
            {
                ShowForm(text);
            });
        }

        public static void Close()
        {
            System.Threading.Thread.Sleep(500);
            if (form != null)
            {
                lock (syncLock)
                {
                    form.CloseForm();
                    form = null;
                }
            }
        }

        private static void ShowForm(string text)
        {
            if (form == null)
            {
                form = new LoadingForm();
                form.SetText(text);
            }
            form.ShowDialog();
        }
    }
}
